<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <header th:replace="header::html"></header>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>角色名
                    </label>
                    <div class="layui-input-block">
                        <input type="text" id="roleName" th:value="${MyRole.roleName}" name="roleName" required="" placeholder="请输入角色名" lay-verify="name" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色描述</label>
                    <div class="layui-input-block">
                        <input type="text" id="description" th:value="${MyRole.description}" name="description" required="" lay-verify="description" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单分配</label>
                    <div class="layui-input-block">
                        <input type="hidden" id="roleId" th:value="${MyRole.roleId}">
                        <ul id="dataTree" class="dtree" data-id="0"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="role-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    layui.use(['dtree','form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let Dtree = null;
        let roleId = $("#roleId").val();
        Dtree = dtree.render({
            elem: "#dataTree",
            initLevel: "1",
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/api/menu/build",
            dataFormat: "list",  //配置data的风格为list
            checkbar: true,  //开启复选框
            checkbarType: "p-casc"
        });
        form.verify({
            name: function(value){
                if(value.length < 2){
                    return '角色名至少2个字符';
                }
            }
        });
        form.on('submit(role-save)', function(data){
            // 获取复选框数组数据
            var paramJsonArr = Dtree.getCheckbarJsonArrParam();
            var ids = paramJsonArr["nodeId"]
            var datas = data.field
            datas['menuIds'] = ids
            datas['roleId'] = roleId
            $.ajax({
                url:'/api/role',
                data:JSON.stringify(datas),
                dataType:'json',
                contentType:'application/json',
                type:'post',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.location.reload();
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>